{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## [pandas入门学习](https://blog.csdn.net/weixin_42398658/article/details/82925410)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用Python的列表创建Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "1    2\n",
       "2    3\n",
       "3    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用 list 创建\n",
    "s1 = pd.Series([1,2,3,4])  # 可以发现索引index默认从0开始进行自动索引\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 值属性，可以方便查看Series的值\n",
    "s1.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=4, step=1)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 索引index属性，返回的是索引从开始到结束和间隔的值\n",
    "s1.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0\n",
       "1    1\n",
       "2    2\n",
       "3    3\n",
       "4    4\n",
       "5    5\n",
       "6    6\n",
       "7    7\n",
       "8    8\n",
       "9    9\n",
       "dtype: int64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用numpy的数组进行创建\n",
    "s2 = pd.Series(np.arange(10))\n",
    "s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1    1\n",
      "2    2\n",
      "3    3\n",
      "dtype: int64\n",
      "[1 2 3]\n",
      "Index(['1', '2', '3'], dtype='object')\n"
     ]
    }
   ],
   "source": [
    "# 通过字典进行创建\n",
    "s3 = pd.Series({'1':1, '2':2, '3':3})\n",
    "print(s3)\n",
    "\n",
    "print(s3.values)\n",
    "\n",
    "print(s3.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A    1\n",
      "B    2\n",
      "C    3\n",
      "D    4\n",
      "dtype: int64\n",
      "[1 2 3 4]\n",
      "Index(['A', 'B', 'C', 'D'], dtype='object')\n",
      "2\n",
      "C    3\n",
      "D    4\n",
      "dtype: int64\n",
      "{'A': 1, 'B': 2, 'C': 3, 'D': 4}\n"
     ]
    }
   ],
   "source": [
    "# 手动赋值索引\n",
    "s4 = pd.Series([1,2,3,4], index=['A','B','C','D'])\n",
    "print(s4)\n",
    "print(s4.values)\n",
    "print(s4.index)\n",
    "\n",
    "# 根据索引取值\n",
    "print(s4['B'])\n",
    "\n",
    "# 根据值得范围取值\n",
    "print(s4[s4>2])\n",
    "\n",
    "# 把Series转换为字典输出，也就是说可以通过字典创建Series，也可以通过Series转换为字典\n",
    "dict_ = s4.to_dict()\n",
    "print(dict_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A    1\n",
      "B    2\n",
      "C    3\n",
      "D    4\n",
      "dtype: int64\n",
      "A    1.0\n",
      "B    2.0\n",
      "C    3.0\n",
      "D    4.0\n",
      "E    NaN\n",
      "dtype: float64\n",
      "A    False\n",
      "B    False\n",
      "C    False\n",
      "D    False\n",
      "E     True\n",
      "dtype: bool\n",
      "A     True\n",
      "B     True\n",
      "C     True\n",
      "D     True\n",
      "E    False\n",
      "dtype: bool\n"
     ]
    }
   ],
   "source": [
    "# 来回转\n",
    "s5 = pd.Series(dict_)\n",
    "print(s5)\n",
    "\n",
    "# 可单独把索引写出，再赋值给Series，同时多增加一个索引\n",
    "index_1 = ['A','B','C','D','E']\n",
    "s6 = pd.Series(s5, index=index_1)\n",
    "print(s6)\n",
    "\n",
    "# 根据pd.isnall()判断Series的元素是否有空值，如果有返回Ture，反之False\n",
    "# 类似的操作 notnull()\n",
    "print(pd.isnull(s6))\n",
    "\n",
    "#s6中元素是否比0大\n",
    "print(s6>0)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
